/*
A simple class for displaying file information and progress
Note: This is a demonstration only and not part of SWFUpload.
Note: Some have had problems adapting this class in IE7. It may not be suitable for your application.
 */
// Constructor
// file is a SWFUpload file object
// targetID is the HTML element id attribute that the FileProgress HTML structure will be added to.
// Instantiating a new FileProgress object with an existing file will reuse/update the existing DOM elements
function FileProgress(file, targetID) {
	this.fileProgressID = file.id;
	this.opacity = 100;
	this.height = 0;
	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;
		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";
		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));
		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(file.name));
		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";
		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";
		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);
		this.fileProgressWrapper.appendChild(this.fileProgressElement);
		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
		this.reset();
	}
	this.height = this.fileProgressWrapper.offsetHeight;
	this.setTimer(null);
}
FileProgress.prototype.setTimer = function(timer) {
	this.fileProgressElement["FP_TIMER"] = timer;
};
FileProgress.prototype.getTimer = function(timer) {
	return this.fileProgressElement["FP_TIMER"] || null;
};
FileProgress.prototype.reset = function() {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[2].innerHTML = "&nbsp;";
	this.fileProgressElement.childNodes[2].className = "progressBarStatus";
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = "0%";
	this.appear();
};
FileProgress.prototype.setProgress = function(percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = percentage + "%";
	this.appear();
};
FileProgress.prototype.setComplete = function() {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].style.width = "";
	var oSelf = this;
	this.setTimer(setTimeout(function() {
		oSelf.disappear();
	}, 10000));
};
FileProgress.prototype.setError = function() {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";
	var oSelf = this;
	this.setTimer(setTimeout(function() {
		oSelf.disappear();
	}, 5000));
};
FileProgress.prototype.setCancelled = function() {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";
	var oSelf = this;
	this.setTimer(setTimeout(function() {
		oSelf.disappear();
	}, 2000));
};
FileProgress.prototype.setStatus = function(status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};
// Show/Hide the cancel button
FileProgress.prototype.toggleCancel = function(show, swfUploadInstance) {
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible"
			: "hidden";
	if (swfUploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].onclick = function() {
			swfUploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};
FileProgress.prototype.appear = function() {
	if (this.getTimer() !== null) {
		clearTimeout(this.getTimer());
		this.setTimer(null);
	}
	if (this.fileProgressWrapper.filters) {
		try {
			this.fileProgressWrapper.filters
					.item("DXImageTransform.Microsoft.Alpha").opacity = 100;
		} catch (e) {
			// If it is not set initially, the browser will throw an error. This
			// will set it if it is not set yet.
			this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
		}
	} else {
		this.fileProgressWrapper.style.opacity = 1;
	}
	this.fileProgressWrapper.style.height = "";
	this.height = this.fileProgressWrapper.offsetHeight;
	this.opacity = 100;
	this.fileProgressWrapper.style.display = "";
};
// Fades out and clips away the FileProgress box.
FileProgress.prototype.disappear = function() {
	var reduceOpacityBy = 15;
	var reduceHeightBy = 4;
	var rate = 30; // 15 fps
	if (this.opacity > 0) {
		this.opacity -= reduceOpacityBy;
		if (this.opacity < 0) {
			this.opacity = 0;
		}
		if (this.fileProgressWrapper.filters) {
			try {
				this.fileProgressWrapper.filters
						.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.
				// This will set it if it is not set yet.
				this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="
						+ this.opacity + ")";
			}
		} else {
			this.fileProgressWrapper.style.opacity = this.opacity / 100;
		}
	}
	if (this.height > 0) {
		this.height -= reduceHeightBy;
		if (this.height < 0) {
			this.height = 0;
		}
		this.fileProgressWrapper.style.height = this.height + "px";
	}
	if (this.height > 0 || this.opacity > 0) {
		var oSelf = this;
		this.setTimer(setTimeout(function() {
			oSelf.disappear();
		}, rate));
	} else {
		this.fileProgressWrapper.style.display = "none";
		this.setTimer(null);
	}
};
/* Demo Note: This demo uses a FileProgress class that handles the UI for displaying the file name and percent complete.
The FileProgress class is not part of SWFUpload.
 */
/* **********************
 Event Handlers
 These are my custom event handlers to make my
 web application behave the way I went when SWFUpload
 completes different tasks. These aren't part of the SWFUpload
 package. They are part of my application. Without these none
 of the actions SWFUpload makes will show up in my application.
 ********************** */
function fileQueued(file) {
	try {
		var progress = new FileProgress(file,
				this.customSettings.progressTarget);
		//progress.setStatus("文件排队中...");
		progress.toggleCancel(true, this);
	} catch (ex) {
		this.debug(ex);
	}
}
function fileQueueError(file, errorCode, message) {
	try {
		if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			alert("上传的文件数太多.\n"
					+ (message === 0 ? "上传的文件数达到上限."
							: "你应该选择"
									+ (message > 1 ? "到" + message
											+ "个文件." : "一个文件.")));
			return;
		}
		var progress = new FileProgress(file,
				this.customSettings.progressTarget);
		progress.setError();
		progress.toggleCancel(false);
		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			progress.setStatus("文件过大.");
			this.debug("错误码:文件过大,文件名: " + file.name
					+ ", 文件大小: " + file.size + ", 消息: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			progress.setStatus("空文件不能上传.");
			this.debug("Error Code: Zero byte file, File name: " + file.name
					+ ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			progress.setStatus("Invalid File Type.");
			this.debug("Error Code: Invalid File Type, File name: " + file.name
					+ ", File size: " + file.size + ", Message: " + message);
			break;
		default:
			if (file !== null) {
				progress.setStatus("Unhandled Error");
			}
			this.debug("Error Code: " + errorCode + ", File name: " + file.name
					+ ", File size: " + file.size + ", Message: " + message);
			break;
		}
	} catch (ex) {
		this.debug(ex);
	}
}
function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesSelected > 0) {
			document.getElementById(this.customSettings.cancelButtonId).disabled = false;
		}
		/* I want auto start the upload and I can do that here */
		this.startUpload();
	} catch (ex) {
		this.debug(ex);
	}
}
function uploadStart(file) {
	try {
		/*
		 * I don't want to do any file validation or anything, I'll just update
		 * the UI and return true to indicate that the upload should start. It's
		 * important to update the UI here because in Linux no uploadProgress
		 * events are called. The best we can do is say we are uploading.
		 */
		var progress = new FileProgress(file,
				this.customSettings.progressTarget);
		progress.setStatus("开始上传...");
		progress.toggleCancel(true, this);
	} catch (ex) {
		this.debug(ex);
	}
	return true;
}
function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
		var progress = new FileProgress(file,
				this.customSettings.progressTarget);
		progress.setProgress(percent);
		progress.setStatus("上传中...");
	} catch (ex) {
		this.debug(ex);
	}
}
function uploadSuccess(file, serverData) {
	try {
		var progress = new FileProgress(file,
				this.customSettings.progressTarget);
		progress.setComplete();
		progress.setStatus("上传完成.");
		progress.toggleCancel(false);
		var jsonStr = $.parseJSON(serverData);
		if(jsonStr.resultCode=="1"){
			var img = jsonStr.file;
			$("#uploadImages").append('<img src="'+ROOTPATH+img+'" alt="图片上传"  style="width: 50px;height: 50px;"/>');
		}else{
			alert(jsonStr.resultStr);
		}
		//alert(jsonStr.resultStr);
	} catch (ex) {
		this.debug(ex);
	}
}
function uploadError(file, errorCode, message) {
	try {
		var progress = new FileProgress(file,
				this.customSettings.progressTarget);
		progress.setError();
		progress.toggleCancel(false);
		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
			progress.setStatus("Upload Error: " + message);
			this.debug("Error Code: HTTP Error, File name: " + file.name
					+ ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
			progress.setStatus("Upload Failed.");
			this.debug("Error Code: Upload Failed, File name: " + file.name
					+ ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.IO_ERROR:
			progress.setStatus("文件IO错误");
			this.debug("错误码: 文件读写出错, 文件名: " + file.name
					+ ", 错误信息: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
			progress.setStatus("Security Error");
			this.debug("Error Code: Security Error, File name: " + file.name
					+ ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			progress.setStatus("Upload limit exceeded.");
			this.debug("Error Code: Upload Limit Exceeded, File name: "
					+ file.name + ", File size: " + file.size + ", Message: "
					+ message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
			progress.setStatus("Failed Validation. Upload skipped.");
			this.debug("Error Code: File Validation Failed, File name: "
					+ file.name + ", File size: " + file.size + ", Message: "
					+ message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			// If there aren't any files left (they were all cancelled) disable
			// the cancel button
			if (this.getStats().files_queued === 0) {
				document.getElementById(this.customSettings.cancelButtonId).disabled = true;
			}
			progress.setStatus("Cancelled");
			progress.setCancelled();
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			progress.setStatus("Stopped");
			break;
		default:
			progress.setStatus("Unhandled Error: " + errorCode);
			this.debug("Error Code: " + errorCode + ", File name: " + file.name
					+ ", File size: " + file.size + ", Message: " + message);
			break;
		}
	} catch (ex) {
		this.debug(ex);
	}
}
function uploadComplete(file) {
	if (this.getStats().files_queued === 0) {
		document.getElementById(this.customSettings.cancelButtonId).disabled = true;
	}
}
// This event comes from the Queue Plugin
function queueComplete(numFilesUploaded) {
	var status = document.getElementById("divStatus");
	status.innerHTML = numFilesUploaded + " file"
			+ (numFilesUploaded === 1 ? "" : "s") + " uploaded.";
}


//信息发布js代码
var sinfoPublisher ={
		id:0,
		//初始化信息发布
		init:function(divId){
			this.id=divId;
			var mainDiv = $("#"+divId);
			mainDiv.html("");
			mainDiv.html(
					'<form action="">'+
					'<table>'+
					'	<tr>'+
					'		<td>信息类型：</td>'+
					'		<td>'+
					'			<select name="sinfoPublishType" id="sinfoPublishType">'+
					'				<option value="0">请选择</option>'+
					'				<option value="longfeng">楼凤</option>'+ 
					'				<option value="sangna">桑拿</option>'+
					'				<option value="kuaican">快餐</option>'+
					'				<option value="xiyu">洗浴</option>'+
					'				<option value="jzmote">兼职模特</option>'+
					'				<option value="jzxuemei">兼职学妹</option>'+
					'				<option value="others">其他</option>'+
					'			</select>'+
					'		</td>'+
					'	</tr>'+
					'	<tr>'+
					'		<td>标题：</td>'+
					'		<td><input type="text" id="sinfoPublishTitle"/></td>'+
					'	</tr>'+
					'	<tr>'+
					'		<td>手机：</td>'+
					'		<td><input type="text" id="sinfoPublishPhone"/></td>'+
					'	</tr>'+
					'	<tr>'+
					'		<td>QQ：</td>'+
					'		<td><input type="text" id="sinfoPublishQQ"/></td>'+
					'	</tr>'+
					'	<tr>'+
					'		<td>邮箱：</td>'+
					'		<td><input type="text" id="sinfoPublishEmail"/></td>'+
					'	</tr>'+
					'	<tr>'+
					'		<td>城市选择：</td>'+
					'		<td><div id="sinfoPublishCitys"></div>'+
					'		<input type="hidden" id="sinfoPublishCity"/>'+
					'		</td>'+
					'	</tr>'+
					'	<tr>'+
					'		<td>城区选择：</td>'+
					'		<td>'+
					'			<select name="sinfoPublishArea" id="sinfoPublishArea">'+
									//这里城市区域
					'				<option value="0">请选择</option>'+
					'			</select>'+
					'		</td>'+
					'	</tr>'+
					'	<tr>'+
					'		<td>信息描述：</td>'+
					'		<td><textarea rows="10" cols="50" id="sinfoPublishDesc"></textarea></td>'+
					'	</tr>'+
					'</table>'+
					'</form>'+
					'<form id="form1" action="index.php" method="post"'+
					'	enctype="multipart/form-data">'+
					'	<div class="fieldset flash" id="fsUploadProgress">'+
					'		<span class="legend">上传队列</span>'+
					'	</div>'+
					'	<div id="divStatus">已上传0个文件</div>'+
					'	<div style="padding-top:2px;">'+
					' 	<table><tr><td>'+
					'		<span id="spanButtonPlaceHolder"></span></td><td>'+
					'       <input id="btnCancel"'+
					'			type="button" value="取消所有已上传"'+
					'			onclick="swfu.cancelQueue();" disabled="disabled"'+
					'			style="margin-left: 2px; margin-top: 0px;font-size: 16; height: 32px;" />'+
					'	</td></tr></table>'+
					'	</div>'+
					'</form>'+
					'<div id="uploadImages">'+
					'</div>'					
			);
			$("#"+divId).dialog({
				autoOpen : false,
				height : 600,
				width : 650,
				modal : true,
				buttons : {
					"提交":function(){
						//信息类型
						var type = $("#sinfoPublishType").val();
						if(type=="0"){
							alert("类型不能为空");
							return;
						}
						var title =$("#sinfoPublishTitle").val();
						if(title==""){
							alert("标题不能为空");
							return;
						}
						var phone =$("#sinfoPublishPhone").val();
						var email =$("#sinfoPublishEmail").val();
						var qq =$("#sinfoPublishQQ").val();
						if(phone=="" && qq==""){
							alert("电话和QQ至少留一种联系方式");
							return ;
						}
						var desc =$("#sinfoPublishDesc").val();
						var cityId = $("#sinfoPublishCity").val();
						var areaId = $("#sinfoPublishArea").val();
						if(cityId=="" || cityId=="0" || areaId=="" || areaId=="0"){
							alert("城市和区县必须选择");
						}
						var images="";
						$("#uploadImages img").each(function(e){
							var img = $(this).attr("src");
							if(images!=""){
								images = images+ ","+ img;
							}else{
								images = img;
							}
						});
						//新增sinfo
						$.post(ROOTPATH + "/postJson/addSInfo.aspx", {
							type:type,email:email,qq:qq,desc:desc,
							title:title,images:images,cityId:cityId,
							phone:phone,areaId:areaId
						}, function(data) {
							if("1"!=data.resultCode){
								alert(data.resultStr);
							}else{
								alert("发布信息成功");
							}
							//这里获得城市信息
							/*$("#sinfoPublishCitys").html("");
							for(var i=0;i<data.list.length;i++){
								if(data.currentCity==data.list[i].id){							
									$("#sinfoPublishCitys").append('<input checked="true" onchange="javascript:sinfoPublisher.selectCity('+data.list[i].id+');" type="radio" name="sinfoPublishCity" value="'+data.list[i].id+'"/>'+data.list[i].cityName);
								}else{
									$("#sinfoPublishCitys").append('<input onchange="javascript:sinfoPublisher.selectCity('+data.list[i].id+');" type="radio" name="sinfoPublishCity" value="'+data.list[i].id+'"/>'+data.list[i].cityName);
								}
							}*/
						});	
						$(this).dialog("close");
					},
					"取消":function(){
						$(this).dialog("close");
					}
				}
			});
			var settings = {
					upload_url : ROOTPATH + "/postJson/uploadFile.aspx",
					flash_url : ROOTPATH + "/upload_js/Flash/swfupload.swf",
					//post_params: {"PHPSESSID" : ""},
					file_size_limit : "10 MB",
					file_post_name : "Filedata",
					file_types : "*.png;*.bmp;*.jpg;*.jpeg;*.gif;*.jpeg;*.tga;",
					file_types_description : "图片文件",
					file_upload_limit : 10,
					file_queue_limit : 0,
					custom_settings : {
						progressTarget : "fsUploadProgress",
						cancelButtonId : "btnCancel"
					},
					debug : false,

					// Button settings
					button_image_url : ROOTPATH + "/img/upload01.png",
					button_width : "70",
					button_height : "30",
					button_placeholder_id : "spanButtonPlaceHolder",
					button_text : '<span class="theFont">上传图片</span>',
					button_text_style : ".theFont { font-size: 16;color: #FF0000; }",
					button_text_left_padding : 0,
					button_text_top_padding : 3,

					// The event handler functions are defined in handlers.js
					file_queued_handler : fileQueued,
					file_queue_error_handler : fileQueueError,
					file_dialog_complete_handler : fileDialogComplete,
					upload_start_handler : uploadStart,
					upload_progress_handler : uploadProgress,
					upload_error_handler : uploadError,
					upload_success_handler : uploadSuccess,
					upload_complete_handler : uploadComplete,
					queue_complete_handler : queueComplete
				// Queue plugin event
				};
				swfu = new SWFUpload(settings);
				
				$.post(ROOTPATH + "/cityJson/findAllCity.aspx", {
				}, function(data) {
					//这里获得城市信息
					$("#sinfoPublishCitys").html("");
					for(var i=0;i<data.list.length;i++){
						if(data.currentCity==data.list[i].id){							
							$("#sinfoPublishCitys").append('<input checked="true" onchange="javascript:sinfoPublisher.selectCity('+data.list[i].id+');" type="radio" name="sinfoPublishCity" value="'+data.list[i].id+'"/>'+data.list[i].cityName);
						}else{
							$("#sinfoPublishCitys").append('<input onchange="javascript:sinfoPublisher.selectCity('+data.list[i].id+');" type="radio" name="sinfoPublishCity" value="'+data.list[i].id+'"/>'+data.list[i].cityName);
						}
					}
				});	
		},
		//
		openDialog:function(){
			$("#"+this.id).dialog("open");
		},
		selectCity:function(id){
			//城市选择
			$("#sinfoPublishCity").val(id);
			alert($("#sinfoPublishCity").val());
			$("#sinfoPublishArea").html("");
			$("#sinfoPublishArea").append('				<option value="0">请选择</option>');
			$.post(ROOTPATH+"/areaJson/queryAreaByCity.aspx",
					 {cityId:id},
					 function(data){
						 //alert(data);
						 //successFunc(data);
						 if(data.list.length<1){
						 }else{
							 for(var i=0;i<data.list.length;i++){
								 $("#sinfoPublishArea").append('				<option value="'+data.list[i].id+'">'+data.list[i].name+'</option>');
							 }
						 }
					 }
				 ); 
		}
};